package com.szh.es.testcase;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

import java.io.IOException;


@Slf4j
public class EsSearchTest {
    public static void main(String[] args) throws IOException {
        // 创建客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200)));

        // 查询所有
        // SearchRequest searchRequest = new SearchRequest("user");
        // SearchSourceBuilder queryBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        // queryBuilder.from(0);
        // queryBuilder.size(4);
        // queryaBuilder.sort("age", SortOrder.DESC);
        // SearchRequest sourceRequest = searchRequest.source(queryBuilder);
        // SearchResponse searchResponse = esClient.search(sourceRequest, RequestOptions.DEFAULT);
        // log.error("getHits:======>{}", searchResponse.getHits().getTotalHits());
        // searchResponse.getHits().forEach(hit -> System.out.println(hit.getSourceAsString()));

        // 2-组合查询

        // SearchRequest searchRequest = new SearchRequest("user");
        // BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //
        // // 这里就是组合条件。和mysql where 组合类似
        // boolQueryBuilder.should(QueryBuilders.matchQuery("age","30"));
        // boolQueryBuilder.should(QueryBuilders.matchQuery("age","40"));
        // SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder);
        // searchRequest.source(sourceBuilder);
        //
        // SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        // searchResponse.getHits().forEach(hit -> System.err.println(hit.getSourceAsString()));

        // 3-范围查询
        // SearchRequest searchRequest = new SearchRequest("user");
        // SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        // rangeQuery.gte("30");
        // sourceBuilder.query(rangeQuery);
        // searchRequest.source(sourceBuilder);
        // SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        // searchResponse.getHits().forEach(hit -> System.out.println(hit.getSourceAsString()));

        //4-模糊查询+高亮
        SearchRequest searchRequest = new SearchRequest("user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name", "张三");
        sourceBuilder.query(fuzzyQuery);
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        sourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(System.out::println);

        // 5-聚合查询

        esClient.close();
    }
}
